package hivi.vn.hivigold.activities;

import hivi.vn.hivigold.R;
import hivi.vn.hivigold.database.Preferences;
import hivi.vn.hivigold.database.WebService;
import hivi.vn.hivigold.objects.Gold;
import hivi.vn.hivigold.utils.Formats;

import java.io.File;
import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.widget.ImageView;
import android.widget.TextView;

public class LCDPresentation extends Activity {
	TextView tv_companytitle;
	TextView tv_companyname;
	TextView tv_tpricename;
	TextView tv_sologan;
	TextView tv_address;
	TextView tv_header1;
	TextView tv_header2;
	TextView tv_header21;
	TextView tv_header22;
	TextView tv_header3;
	TextView tv_wellcome;

	TextView tv_price11;
	TextView tv_price12;
	TextView tv_price13;
	TextView tv_pricename1;
	TextView tv_price21;
	TextView tv_price22;
	TextView tv_price23;
	TextView tv_pricename2;
	TextView tv_price31;
	TextView tv_price32;
	TextView tv_price33;
	TextView tv_pricename3;

	ImageView vi_logo;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.lcdpresentation);

		tv_companytitle = (TextView) findViewById(R.id.tv_companytitle);
		tv_companyname = (TextView) findViewById(R.id.tv_companyname);
		tv_tpricename = (TextView) findViewById(R.id.tv_tpricename);
		tv_address = (TextView) findViewById(R.id.tv_address);
		tv_sologan = (TextView) findViewById(R.id.tv_sologan);
		tv_header1 = (TextView) findViewById(R.id.tv_header1);
		tv_header2 = (TextView) findViewById(R.id.tv_header2);
		tv_header21 = (TextView) findViewById(R.id.tv_header21);
		tv_header22 = (TextView) findViewById(R.id.tv_header22);
		tv_header3 = (TextView) findViewById(R.id.tv_header3);
		tv_wellcome = (TextView) findViewById(R.id.tv_wellcome);

		tv_price11 = (TextView) findViewById(R.id.tv_price11);
		tv_price12 = (TextView) findViewById(R.id.tv_price12);
		tv_price13 = (TextView) findViewById(R.id.tv_price13);
		tv_pricename1 = (TextView) findViewById(R.id.tv_pricename1);
		tv_price21 = (TextView) findViewById(R.id.tv_price21);
		tv_price22 = (TextView) findViewById(R.id.tv_price22);
		tv_price23 = (TextView) findViewById(R.id.tv_price23);
		tv_pricename2 = (TextView) findViewById(R.id.tv_pricename2);
		tv_price31 = (TextView) findViewById(R.id.tv_price31);
		tv_price32 = (TextView) findViewById(R.id.tv_price32);
		tv_price33 = (TextView) findViewById(R.id.tv_price33);
		tv_pricename3 = (TextView) findViewById(R.id.tv_pricename3);

		vi_logo = (ImageView) findViewById(R.id.vi_logo);

		loadConfigData();
		loadPriceData();
		Handler locationHandler = new Handler() {
			public void handleMessage(Message msg) {
				if (msg.what == 0) {
					loadPriceData();
					this.sendEmptyMessageDelayed(0, 30000);
				}
			}
		};
		locationHandler.sendEmptyMessage(0);
	}

	protected void loadConfigData() {
		Bitmap processedBitmap = processingLogo();
		if (processedBitmap != null) {
			vi_logo.setImageBitmap(processedBitmap);
		}

		tv_companytitle.setText(Preferences.getPreferenceValue(this,
				Preferences.COMPANYTITLE).toString());
		tv_companyname.setText(Preferences.getPreferenceValue(this,
				Preferences.COMPANYNAME).toString());
		tv_tpricename.setText(Preferences.getPreferenceValue(this,
				Preferences.TPRICENAME).toString());
		tv_address.setText(Preferences.getPreferenceValue(this,
				Preferences.ADDRESS).toString());
		tv_sologan.setText(Preferences.getPreferenceValue(this,
				Preferences.SOLOGAN).toString());
		tv_header1.setText(Preferences.getPreferenceValue(this,
				Preferences.HEADER1).toString());
		tv_header2.setText(Preferences.getPreferenceValue(this,
				Preferences.HEADER2).toString());
		tv_header21.setText(Preferences.getPreferenceValue(this,
				Preferences.HEADER21).toString());
		tv_header22.setText(Preferences.getPreferenceValue(this,
				Preferences.HEADER22).toString());
		tv_header3.setText(Preferences.getPreferenceValue(this,
				Preferences.HEADER3).toString());
		tv_wellcome.setText(Preferences.getPreferenceValue(this,
				Preferences.WELLCOME).toString());
	}

	protected void loadPriceData() {
		String[] priceData = Preferences.getPreferenceValue(this,
				Preferences.PRICEDATA).split(",");
		ArrayList<Gold> arrGold = WebService.getGoldPrice(this);

		for (int i = 0; i < priceData.length; i++) {
			if (i == 0) {
				tv_pricename1.setText("");
				for (int j = 0; j < arrGold.size(); j++) {
					if (arrGold.get(j).getTienteID().matches(priceData[i])) {
						tv_pricename1.setText(arrGold.get(j).getTienteName2());
						break;
					}

				}
			}
			if (i == 1) {
				String[] typePrice = priceData[i].split(":");
				tv_price11.setText("");
				for (int j = 0; j < arrGold.size(); j++) {
					if (arrGold.get(j).getTienteID().matches(typePrice[0])) {
						if (typePrice[1].matches("1")) {
							tv_price11.setText(Formats.money(arrGold.get(j)
									.getMua_vao()));
						}
						if (typePrice[1].matches("2")) {
							tv_price11.setText(Formats.money(arrGold.get(j)
									.getBan_ra()));
						}
						break;
					}
				}
			}
			if (i == 2) {
				String[] typePrice = priceData[i].split(":");
				tv_price12.setText("");
				for (int j = 0; j < arrGold.size(); j++) {
					if (arrGold.get(j).getTienteID().matches(typePrice[0])) {
						if (typePrice[1].matches("1")) {
							tv_price12.setText(Formats.money(arrGold.get(j)
									.getMua_vao()));
						}
						if (typePrice[1].matches("2")) {
							tv_price12.setText(Formats.money(arrGold.get(j)
									.getBan_ra()));
						}
						break;
					}
				}
			}
			if (i == 3) {
				String[] typePrice = priceData[i].split(":");
				tv_price13.setText("");
				for (int j = 0; j < arrGold.size(); j++) {
					if (arrGold.get(j).getTienteID().matches(typePrice[0])) {
						if (typePrice[1].matches("1")) {
							tv_price13.setText(Formats.money(arrGold.get(j)
									.getMua_vao()));
						}
						if (typePrice[1].matches("2")) {
							tv_price13.setText(Formats.money(arrGold.get(j)
									.getBan_ra()));
						}
						break;
					}
				}
			}
			if (i == 4) {
				tv_pricename2.setText("");
				for (int j = 0; j < arrGold.size(); j++) {
					if (arrGold.get(j).getTienteID().matches(priceData[i])) {
						tv_pricename2.setText(arrGold.get(j).getTienteName2());
						break;
					}

				}
			}
			if (i == 5) {
				String[] typePrice = priceData[i].split(":");
				tv_price21.setText("");
				for (int j = 0; j < arrGold.size(); j++) {
					if (arrGold.get(j).getTienteID().matches(typePrice[0])) {
						if (typePrice[1].matches("1")) {
							tv_price21.setText(Formats.money(arrGold.get(j)
									.getMua_vao()));
						}
						if (typePrice[1].matches("2")) {
							tv_price21.setText(Formats.money(arrGold.get(j)
									.getBan_ra()));
						}
						break;
					}
				}
			}
			if (i == 6) {
				String[] typePrice = priceData[i].split(":");
				tv_price22.setText("");
				for (int j = 0; j < arrGold.size(); j++) {
					if (arrGold.get(j).getTienteID().matches(typePrice[0])) {
						if (typePrice[1].matches("1")) {
							tv_price22.setText(Formats.money(arrGold.get(j)
									.getMua_vao()));
						}
						if (typePrice[1].matches("2")) {
							tv_price22.setText(Formats.money(arrGold.get(j)
									.getBan_ra()));
						}
						break;
					}
				}
			}
			if (i == 7) {
				String[] typePrice = priceData[i].split(":");
				tv_price23.setText("");
				for (int j = 0; j < arrGold.size(); j++) {
					if (arrGold.get(j).getTienteID().matches(typePrice[0])) {
						if (typePrice[1].matches("1")) {
							tv_price23.setText(Formats.money(arrGold.get(j)
									.getMua_vao()));
						}
						if (typePrice[1].matches("2")) {
							tv_price23.setText(Formats.money(arrGold.get(j)
									.getBan_ra()));
						}
						break;
					}
				}
			}
			if (i == 8) {
				tv_pricename3.setText("");
				for (int j = 0; j < arrGold.size(); j++) {
					if (arrGold.get(j).getTienteID().matches(priceData[i])) {
						tv_pricename3.setText(arrGold.get(j).getTienteName2());
						break;
					}

				}
			}
			if (i == 9) {
				String[] typePrice = priceData[i].split(":");
				tv_price31.setText("");
				for (int j = 0; j < arrGold.size(); j++) {
					if (arrGold.get(j).getTienteID().matches(typePrice[0])) {
						if (typePrice[1].matches("1")) {
							tv_price31.setText(Formats.money(arrGold.get(j)
									.getMua_vao()));
						}
						if (typePrice[1].matches("2")) {
							tv_price31.setText(Formats.money(arrGold.get(j)
									.getBan_ra()));
						}
						break;
					}
				}
			}
			if (i == 10) {
				String[] typePrice = priceData[i].split(":");
				tv_price32.setText("");
				for (int j = 0; j < arrGold.size(); j++) {
					if (arrGold.get(j).getTienteID().matches(typePrice[0])) {
						if (typePrice[1].matches("1")) {
							tv_price32.setText(Formats.money(arrGold.get(j)
									.getMua_vao()));
						}
						if (typePrice[1].matches("2")) {
							tv_price32.setText(Formats.money(arrGold.get(j)
									.getBan_ra()));
						}
						break;
					}
				}
			}
			if (i == 11) {
				String[] typePrice = priceData[i].split(":");
				tv_price33.setText("");
				for (int j = 0; j < arrGold.size(); j++) {
					if (arrGold.get(j).getTienteID().matches(typePrice[0])) {
						if (typePrice[1].matches("1")) {
							tv_price33.setText(Formats.money(arrGold.get(j)
									.getMua_vao()));
						}
						if (typePrice[1].matches("2")) {
							tv_price33.setText(Formats.money(arrGold.get(j)
									.getBan_ra()));
						}
						break;
					}
				}
			}
		}

	}

	private Bitmap processingLogo() {
		File imgFile = new File(Preferences.getPreferenceValue(this,
				Preferences.LOGO));
		if (imgFile.exists()) {
			Bitmap myBitmap = BitmapFactory.decodeFile(imgFile
					.getAbsolutePath());
			return myBitmap;
		} else
			return null;
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
	}

	public static void onStartIntent(Context c) {
		Intent i = new Intent(c, LCDPresentation.class);
		i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		c.startActivity(i);
	}
}
